if (files.cwd().endsWith("脚本")) {
  let _require = require;
  require = function (path) {
    if (path.startsWith("./")) {
      path = path.replace("./", "");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}

auto.waitFor();
var utils = require("./utils.js");

var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000)

function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!")
    while (true) {
      var allow = text("允许").findOne(5000)
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!")
        allow.click()
        sleep(1000)
        thread.interrupt()
        break
      }
    }
  })
  sleep(1000)
  var result = launchApp(app_name)
  sleep(3000)
  thread.interrupt()
  return result
}

function task() {
  var index = 0
  while (index < 15) {
    var 快速注册 = text('快速注册').findOne(1000)
    if (快速注册) {
      sleep(time)
      var 登录 = text('登录').findOne(1000)
      if (登录) {
        登录.click()
        break
      }
    }
    index++
  }
  utils.updateAppProgress(30);
  var 首页 = text('首页').findOne(20000)
  if (首页) {
    var 确定 = text('确定').findOne(5000)
    if (确定) {
      确定.click()
      sleep(time)
    }
    utils.updateAppProgress(50);
    var 我的 = text('我的').findOne(5000)
    if (我的) {
      我的.parent().parent().click()
      utils.updateAppProgress(80);
      sleep(time)
      var 签到 = text('签到').findOne(5000)
      if (签到) {
        签到.parent().click()
        sleep(time)
      }
    }
  }
}

function main() {
  let open_app = launchAppEnhance("善盾");
  if (open_app) {
    task()
  }
  else {
    toastLog("善盾打开失败，请确认是否安装并授权打开!");
  }
}

try {
  main();
  utils.updateAppProgress(100);
} catch (error) {
  toastLog(error);
}

storage.put("script_running_status", "end");
